package com.crocodileQ.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crocodileQ.common.PageResult;
import com.crocodileQ.common.R;
import com.crocodileQ.constants.SystemConstants;
import com.crocodileQ.domain.VO.ArticleListVO;
import com.crocodileQ.domain.VO.AtlasVO;
import com.crocodileQ.domain.entity.Article;
import com.crocodileQ.domain.entity.Atlas;
import com.crocodileQ.mapper.AtlasMapper;
import com.crocodileQ.utils.BeanCopyUtil;
import org.springframework.stereotype.Service;
import com.crocodileQ.service.AtlasService;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * (Atlas)表服务实现类
 *
 * @author makejava
 * @since 2024-01-31 21:57:39
 */
@Service("atlasService")
public class AtlasServiceImpl extends ServiceImpl<AtlasMapper, Atlas> implements AtlasService {


    /**
     * 分页查询图片
     * @param pageNum 1
     * @param pageSize 6
     * @param categoryId 1
     * @param name like
     * @return ArticleListVO
     */
    public R<PageResult<AtlasVO>> atlasList(Integer pageNum, Integer pageSize, Long categoryId, String name) {

       //分页查询图片
        //查询条件
        LambdaQueryWrapper<Atlas> queryWrapper = new LambdaQueryWrapper<>();
        //如果有categoryId,查询时判断
        queryWrapper.eq(Objects.nonNull(categoryId)&&categoryId>0,Atlas::getCategoryId,categoryId);
        queryWrapper.like(StringUtils.hasText(name),Atlas::getName,name);

        //对isTop进行降序排序
        queryWrapper.orderByDesc(Atlas::getIsTop);

        //分页查询
        Page<Atlas> page = new Page<>(pageNum, pageSize);
        page(page,queryWrapper);

        List<Atlas> list = page.getRecords();

        //处理分类名称 冗余

        //处理成AtlasVO,返回
        List<AtlasVO> atlasVOS = BeanCopyUtil.copyBeanList(list, AtlasVO.class);
        PageResult<AtlasVO> pageResult = new PageResult<>(atlasVOS, page.getTotal());

        return R.success(pageResult);
    }
}

